
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>错误处理 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="18_Type_Casting.html" />
    
    
    <link rel="prev" href="16_Optional_Chaining.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01_The_Basics.html">
            
                <a href="01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02_Basic_Operators.html">
            
                <a href="02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03_Strings_and_Characters.html">
            
                <a href="03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04_Collection_Types.html">
            
                <a href="04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="05_Control_Flow.html">
            
                <a href="05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="06_Functions.html">
            
                <a href="06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="07_Closures.html">
            
                <a href="07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="08_Enumerations.html">
            
                <a href="08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="09_Structures_And_Classes.html">
            
                <a href="09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="10_Properties.html">
            
                <a href="10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="11_Methods.html">
            
                <a href="11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="12_Subscripts.html">
            
                <a href="12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="13_Inheritance.html">
            
                <a href="13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="14_Initialization.html">
            
                <a href="14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="15_Deinitialization.html">
            
                <a href="15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="16_Optional_Chaining.html">
            
                <a href="16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.17" data-path="17_Error_Handling.html">
            
                <a href="17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="18_Type_Casting.html">
            
                <a href="18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="19_Nested_Types.html">
            
                <a href="19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="20_Extensions.html">
            
                <a href="20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="21_Protocols.html">
            
                <a href="21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="22_Generics.html">
            
                <a href="22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="23_Automatic_Reference_Counting.html">
            
                <a href="23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="24_Memory_Safety.html">
            
                <a href="24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="25_Access_Control.html">
            
                <a href="25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="26_Advanced_Operators.html">
            
                <a href="26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >错误处理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x9519;&#x8BEF;&#x5904;&#x7406;">&#x9519;&#x8BEF;&#x5904;&#x7406;</h1>
<p><em>&#x9519;&#x8BEF;&#x5904;&#x7406;&#xFF08;Error handling&#xFF09;</em> &#x662F;&#x54CD;&#x5E94;&#x9519;&#x8BEF;&#x4EE5;&#x53CA;&#x4ECE;&#x9519;&#x8BEF;&#x4E2D;&#x6062;&#x590D;&#x7684;&#x8FC7;&#x7A0B;&#x3002;Swift &#x5728;&#x8FD0;&#x884C;&#x65F6;&#x63D0;&#x4F9B;&#x4E86;&#x629B;&#x51FA;&#x3001;&#x6355;&#x83B7;&#x3001;&#x4F20;&#x9012;&#x548C;&#x64CD;&#x4F5C;&#x53EF;&#x6062;&#x590D;&#x9519;&#x8BEF;&#xFF08;recoverable errors&#xFF09;&#x7684;&#x4E00;&#x7B49;&#x652F;&#x6301;&#xFF08;first-class support&#xFF09;&#x3002;</p>
<p>&#x67D0;&#x4E9B;&#x64CD;&#x4F5C;&#x65E0;&#x6CD5;&#x4FDD;&#x8BC1;&#x603B;&#x662F;&#x6267;&#x884C;&#x5B8C;&#x6240;&#x6709;&#x4EE3;&#x7801;&#x6216;&#x751F;&#x6210;&#x6709;&#x7528;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7528;&#x6765;&#x8868;&#x793A;&#x503C;&#x7F3A;&#x5931;&#xFF0C;&#x4F46;&#x662F;&#x5F53;&#x67D0;&#x4E2A;&#x64CD;&#x4F5C;&#x5931;&#x8D25;&#x65F6;&#xFF0C;&#x7406;&#x89E3;&#x9020;&#x6210;&#x5931;&#x8D25;&#x7684;&#x539F;&#x56E0;&#x6709;&#x52A9;&#x4E8E;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x4F5C;&#x51FA;&#x76F8;&#x5E94;&#x7684;&#x5E94;&#x5BF9;&#x3002;</p>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x5047;&#x5982;&#x6709;&#x4E2A;&#x4ECE;&#x78C1;&#x76D8;&#x4E0A;&#x7684;&#x67D0;&#x4E2A;&#x6587;&#x4EF6;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x5E76;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x8BE5;&#x4EFB;&#x52A1;&#x4F1A;&#x6709;&#x591A;&#x79CD;&#x53EF;&#x80FD;&#x5931;&#x8D25;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x5305;&#x62EC;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x4E0B;&#x6587;&#x4EF6;&#x5E76;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x6587;&#x4EF6;&#x4E0D;&#x5177;&#x6709;&#x53EF;&#x8BFB;&#x6743;&#x9650;&#xFF0C;&#x6216;&#x8005;&#x6587;&#x4EF6;&#x7F16;&#x7801;&#x683C;&#x5F0F;&#x4E0D;&#x517C;&#x5BB9;&#x3002;&#x533A;&#x5206;&#x8FD9;&#x4E9B;&#x4E0D;&#x540C;&#x7684;&#x5931;&#x8D25;&#x60C5;&#x51B5;&#x53EF;&#x4EE5;&#x8BA9;&#x7A0B;&#x5E8F;&#x5904;&#x7406;&#x5E76;&#x89E3;&#x51B3;&#x67D0;&#x4E9B;&#x9519;&#x8BEF;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x5B83;&#x89E3;&#x51B3;&#x4E0D;&#x4E86;&#x7684;&#x9519;&#x8BEF;&#x62A5;&#x544A;&#x7ED9;&#x7528;&#x6237;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>Swift &#x4E2D;&#x7684;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x6D89;&#x53CA;&#x5230;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x6A21;&#x5F0F;&#xFF0C;&#x8FD9;&#x4F1A;&#x7528;&#x5230; Cocoa &#x548C; Objective-C &#x4E2D;&#x7684; <code>NSError</code>&#x3002;&#x66F4;&#x591A;&#x8BE6;&#x60C5;&#x53C2;&#x89C1; <a href="https://developer.apple.com/documentation/swift/cocoa_design_patterns/handling_cocoa_errors_in_swift" target="_blank">&#x7528; Swift &#x89E3;&#x51B3; Cocoa &#x9519;&#x8BEF;</a>&#x3002;</p>
</blockquote>
<h2 id="representing-and-throwing-errors">&#x8868;&#x793A;&#x4E0E;&#x629B;&#x51FA;&#x9519;&#x8BEF; </h2>
<p>&#x5728; Swift &#x4E2D;&#xFF0C;&#x9519;&#x8BEF;&#x7528;&#x9075;&#x5FAA; <code>Error</code> &#x534F;&#x8BAE;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x6765;&#x8868;&#x793A;&#x3002;&#x8FD9;&#x4E2A;&#x7A7A;&#x534F;&#x8BAE;&#x8868;&#x660E;&#x8BE5;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x3002;</p>
<p>Swift &#x7684;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x5C24;&#x4E3A;&#x9002;&#x5408;&#x6784;&#x5EFA;&#x4E00;&#x7EC4;&#x76F8;&#x5173;&#x7684;&#x9519;&#x8BEF;&#x72B6;&#x6001;&#xFF0C;&#x679A;&#x4E3E;&#x7684;&#x5173;&#x8054;&#x503C;&#x8FD8;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x9519;&#x8BEF;&#x72B6;&#x6001;&#x7684;&#x989D;&#x5916;&#x4FE1;&#x606F;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x6E38;&#x620F;&#x4E2D;&#x64CD;&#x4F5C;&#x81EA;&#x52A8;&#x8D29;&#x5356;&#x673A;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x8868;&#x793A;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#x7684;&#x9519;&#x8BEF;&#x72B6;&#x6001;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">VendingMachineError</span>: <span class="hljs-title">Error</span> </span>{
    <span class="hljs-keyword">case</span> invalidSelection                     <span class="hljs-comment">//&#x9009;&#x62E9;&#x65E0;&#x6548;</span>
    <span class="hljs-keyword">case</span> insufficientFunds(coinsNeeded: <span class="hljs-type">Int</span>) <span class="hljs-comment">//&#x91D1;&#x989D;&#x4E0D;&#x8DB3;</span>
    <span class="hljs-keyword">case</span> outOfStock                             <span class="hljs-comment">//&#x7F3A;&#x8D27;</span>
}
</code></pre>
<p>&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x8868;&#x660E;&#x6709;&#x610F;&#x5916;&#x60C5;&#x51B5;&#x53D1;&#x751F;&#xFF0C;&#x5BFC;&#x81F4;&#x6B63;&#x5E38;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;&#x65E0;&#x6CD5;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x3002;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x4F7F;&#x7528; <code>throw</code> &#x8BED;&#x53E5;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x63D0;&#x793A;&#x8D29;&#x5356;&#x673A;&#x8FD8;&#x9700;&#x8981; <code>5</code> &#x4E2A;&#x786C;&#x5E01;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">throw</span> <span class="hljs-type">VendingMachineError</span>.insufficientFunds(coinsNeeded: <span class="hljs-number">5</span>)
</code></pre>
<h2 id="handling-errors">&#x5904;&#x7406;&#x9519;&#x8BEF; </h2>
<p>&#x67D0;&#x4E2A;&#x9519;&#x8BEF;&#x88AB;&#x629B;&#x51FA;&#x65F6;&#xFF0C;&#x9644;&#x8FD1;&#x7684;&#x67D0;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x5FC5;&#x987B;&#x8D1F;&#x8D23;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x4F8B;&#x5982;&#x7EA0;&#x6B63;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3001;&#x5C1D;&#x8BD5;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3001;&#x6216;&#x662F;&#x5411;&#x7528;&#x6237;&#x62A5;&#x544A;&#x9519;&#x8BEF;&#x3002;</p>
<p>Swift &#x4E2D;&#x6709; <code>4</code> &#x79CD;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x7684;&#x65B9;&#x5F0F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x51FD;&#x6570;&#x629B;&#x51FA;&#x7684;&#x9519;&#x8BEF;&#x4F20;&#x9012;&#x7ED9;&#x8C03;&#x7528;&#x6B64;&#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#x3001;&#x7528; <code>do-catch</code> &#x8BED;&#x53E5;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x3001;&#x5C06;&#x9519;&#x8BEF;&#x4F5C;&#x4E3A;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x5904;&#x7406;&#x3001;&#x6216;&#x8005;&#x65AD;&#x8A00;&#x6B64;&#x9519;&#x8BEF;&#x6839;&#x672C;&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x3002;&#x6BCF;&#x79CD;&#x65B9;&#x5F0F;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x5C0F;&#x8282;&#x4E2D;&#x90FD;&#x6709;&#x63CF;&#x8FF0;&#x3002;</p>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x65F6;&#xFF0C;&#x4F60;&#x7684;&#x7A0B;&#x5E8F;&#x6D41;&#x7A0B;&#x4F1A;&#x53D1;&#x751F;&#x6539;&#x53D8;&#xFF0C;&#x6240;&#x4EE5;&#x91CD;&#x8981;&#x7684;&#x662F;&#x4F60;&#x80FD;&#x8FC5;&#x901F;&#x8BC6;&#x522B;&#x4EE3;&#x7801;&#x4E2D;&#x4F1A;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x5730;&#x65B9;&#x3002;&#x4E3A;&#x4E86;&#x6807;&#x8BC6;&#x51FA;&#x8FD9;&#x4E9B;&#x5730;&#x65B9;&#xFF0C;&#x5728;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x80FD;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x51FD;&#x6570;&#x3001;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x6784;&#x9020;&#x5668;&#x4E4B;&#x524D;&#xFF0C;&#x52A0;&#x4E0A; <code>try</code> &#x5173;&#x952E;&#x5B57;&#xFF0C;&#x6216;&#x8005; <code>try?</code> &#x6216; <code>try!</code> &#x8FD9;&#x79CD;&#x53D8;&#x4F53;&#x3002;&#x8FD9;&#x4E9B;&#x5173;&#x952E;&#x5B57;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x5C0F;&#x8282;&#x4E2D;&#x6709;&#x5177;&#x4F53;&#x8BB2;&#x89E3;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>Swift &#x4E2D;&#x7684;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x548C;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x4E2D;&#x7528; <code>try</code>&#xFF0C;<code>catch</code> &#x548C; <code>throw</code> &#x8FDB;&#x884C;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x5F88;&#x50CF;&#x3002;&#x548C;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x4E2D;&#xFF08;&#x5305;&#x62EC; Objective-C &#xFF09;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;Swift &#x4E2D;&#x7684;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x5E76;&#x4E0D;&#x6D89;&#x53CA;&#x89E3;&#x9664;&#x8C03;&#x7528;&#x6808;&#xFF0C;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x8BA1;&#x7B97;&#x4EE3;&#x4EF7;&#x9AD8;&#x6602;&#x7684;&#x8FC7;&#x7A0B;&#x3002;&#x5C31;&#x6B64;&#x800C;&#x8A00;&#xFF0C;<code>throw</code> &#x8BED;&#x53E5;&#x7684;&#x6027;&#x80FD;&#x7279;&#x6027;&#x662F;&#x53EF;&#x4EE5;&#x548C; <code>return</code> &#x8BED;&#x53E5;&#x76F8;&#x5AB2;&#x7F8E;&#x7684;&#x3002;</p>
</blockquote>
<h3 id="propagating-errors-using-throwing-functions">&#x7528; throwing &#x51FD;&#x6570;&#x4F20;&#x9012;&#x9519;&#x8BEF; </h3>
<p>&#x4E3A;&#x4E86;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3001;&#x65B9;&#x6CD5;&#x6216;&#x6784;&#x9020;&#x5668;&#x53EF;&#x4EE5;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x7684;&#x53C2;&#x6570;&#x4E4B;&#x540E;&#x52A0;&#x4E0A; <code>throws</code> &#x5173;&#x952E;&#x5B57;&#x3002;&#x4E00;&#x4E2A;&#x6807;&#x6709; <code>throws</code> &#x5173;&#x952E;&#x5B57;&#x7684;&#x51FD;&#x6570;&#x88AB;&#x79F0;&#x4F5C; <em>throwing &#x51FD;&#x6570;</em>&#x3002;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6307;&#x660E;&#x4E86;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#xFF0C;<code>throws</code> &#x5173;&#x952E;&#x8BCD;&#x9700;&#x8981;&#x5199;&#x5728;&#x8FD4;&#x56DE;&#x7BAD;&#x5934;&#xFF08;<code>-&gt;</code>&#xFF09;&#x7684;&#x524D;&#x9762;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">canThrowErrors</span><span class="hljs-params">()</span></span> <span class="hljs-keyword">throws</span> -&gt; <span class="hljs-type">String</span>

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">cannotThrowErrors</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span>
</code></pre>
<p>&#x4E00;&#x4E2A; throwing &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5728;&#x5176;&#x5185;&#x90E8;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x5E76;&#x5C06;&#x9519;&#x8BEF;&#x4F20;&#x9012;&#x5230;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x53EA;&#x6709; throwing &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x9519;&#x8BEF;&#x3002;&#x4EFB;&#x4F55;&#x5728;&#x67D0;&#x4E2A;&#x975E; throwing &#x51FD;&#x6570;&#x5185;&#x90E8;&#x629B;&#x51FA;&#x7684;&#x9519;&#x8BEF;&#x53EA;&#x80FD;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5904;&#x7406;&#x3002;</p>
</blockquote>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>VendingMachine</code> &#x7C7B;&#x6709;&#x4E00;&#x4E2A; <code>vend(itemNamed:)</code> &#x65B9;&#x6CD5;&#xFF0C;&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x7684;&#x7269;&#x54C1;&#x4E0D;&#x5B58;&#x5728;&#x3001;&#x7F3A;&#x8D27;&#x6216;&#x8005;&#x6295;&#x5165;&#x91D1;&#x989D;&#x5C0F;&#x4E8E;&#x7269;&#x54C1;&#x4EF7;&#x683C;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x76F8;&#x5E94;&#x7684; <code>VendingMachineError</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Item</span> </span>{
    <span class="hljs-keyword">var</span> price: <span class="hljs-type">Int</span>
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">count</span>: <span class="hljs-type">Int</span>
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">VendingMachine</span> </span>{
    <span class="hljs-keyword">var</span> inventory = [
        <span class="hljs-string">&quot;Candy Bar&quot;</span>: <span class="hljs-type">Item</span>(price: <span class="hljs-number">12</span>, <span class="hljs-built_in">count</span>: <span class="hljs-number">7</span>),
        <span class="hljs-string">&quot;Chips&quot;</span>: <span class="hljs-type">Item</span>(price: <span class="hljs-number">10</span>, <span class="hljs-built_in">count</span>: <span class="hljs-number">4</span>),
        <span class="hljs-string">&quot;Pretzels&quot;</span>: <span class="hljs-type">Item</span>(price: <span class="hljs-number">7</span>, <span class="hljs-built_in">count</span>: <span class="hljs-number">11</span>)
    ]
    <span class="hljs-keyword">var</span> coinsDeposited = <span class="hljs-number">0</span>

    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">vend</span><span class="hljs-params">(itemNamed name: String)</span></span> <span class="hljs-keyword">throws</span> {
        <span class="hljs-keyword">guard</span> <span class="hljs-keyword">let</span> item = inventory[name] <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">throw</span> <span class="hljs-type">VendingMachineError</span>.invalidSelection
        }

        <span class="hljs-keyword">guard</span> item.<span class="hljs-built_in">count</span> &gt; <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">throw</span> <span class="hljs-type">VendingMachineError</span>.outOfStock
        }

        <span class="hljs-keyword">guard</span> item.price &lt;= coinsDeposited <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">throw</span> <span class="hljs-type">VendingMachineError</span>.insufficientFunds(coinsNeeded: item.price - coinsDeposited)
        }

        coinsDeposited -= item.price

        <span class="hljs-keyword">var</span> newItem = item
        newItem.<span class="hljs-built_in">count</span> -= <span class="hljs-number">1</span>
        inventory[name] = newItem

        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Dispensing <span class="hljs-subst">\(name)</span>&quot;</span>)
    }
}
</code></pre>
<p>&#x5728; <code>vend(itemNamed:)</code> &#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x4E2D;&#x4F7F;&#x7528;&#x4E86; <code>guard</code> &#x8BED;&#x53E5;&#x6765;&#x786E;&#x4FDD;&#x5728;&#x8D2D;&#x4E70;&#x67D0;&#x4E2A;&#x7269;&#x54C1;&#x6240;&#x9700;&#x7684;&#x6761;&#x4EF6;&#x4E2D;&#x6709;&#x4EFB;&#x4E00;&#x6761;&#x4EF6;&#x4E0D;&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x80FD;&#x63D0;&#x524D;&#x9000;&#x51FA;&#x65B9;&#x6CD5;&#x5E76;&#x629B;&#x51FA;&#x76F8;&#x5E94;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x7531;&#x4E8E; <code>throw</code> &#x8BED;&#x53E5;&#x4F1A;&#x7ACB;&#x5373;&#x9000;&#x51FA;&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x7269;&#x54C1;&#x53EA;&#x6709;&#x5728;&#x6240;&#x6709;&#x6761;&#x4EF6;&#x90FD;&#x6EE1;&#x8DB3;&#x65F6;&#x624D;&#x4F1A;&#x88AB;&#x552E;&#x51FA;&#x3002;</p>
<p>&#x56E0;&#x4E3A; <code>vend(itemNamed:)</code> &#x65B9;&#x6CD5;&#x4F1A;&#x4F20;&#x9012;&#x51FA;&#x5B83;&#x629B;&#x51FA;&#x7684;&#x4EFB;&#x4F55;&#x9519;&#x8BEF;&#xFF0C;&#x5728;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x4E48;&#x76F4;&#x63A5;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x9519;&#x8BEF;&#x2014;&#x2014;&#x4F7F;&#x7528; <code>do-catch</code> &#x8BED;&#x53E5;&#xFF0C;<code>try?</code> &#x6216; <code>try!</code>&#xFF1B;&#x8981;&#x4E48;&#x7EE7;&#x7EED;&#x5C06;&#x8FD9;&#x4E9B;&#x9519;&#x8BEF;&#x4F20;&#x9012;&#x4E0B;&#x53BB;&#x3002;&#x4F8B;&#x5982;&#x4E0B;&#x9762;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>buyFavoriteSnack(person:vendingMachine:)</code> &#x540C;&#x6837;&#x662F;&#x4E00;&#x4E2A; throwing &#x51FD;&#x6570;&#xFF0C;&#x4EFB;&#x4F55;&#x7531; <code>vend(itemNamed:)</code> &#x65B9;&#x6CD5;&#x629B;&#x51FA;&#x7684;&#x9519;&#x8BEF;&#x4F1A;&#x4E00;&#x76F4;&#x88AB;&#x4F20;&#x9012;&#x5230; <code>buyFavoriteSnack(person:vendingMachine:)</code> &#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x5730;&#x65B9;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> favoriteSnacks = [
    <span class="hljs-string">&quot;Alice&quot;</span>: <span class="hljs-string">&quot;Chips&quot;</span>,
    <span class="hljs-string">&quot;Bob&quot;</span>: <span class="hljs-string">&quot;Licorice&quot;</span>,
    <span class="hljs-string">&quot;Eve&quot;</span>: <span class="hljs-string">&quot;Pretzels&quot;</span>,
]
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">buyFavoriteSnack</span><span class="hljs-params">(person: String, vendingMachine: VendingMachine)</span></span> <span class="hljs-keyword">throws</span> {
    <span class="hljs-keyword">let</span> snackName = favoriteSnacks[person] ?? <span class="hljs-string">&quot;Candy Bar&quot;</span>
    <span class="hljs-keyword">try</span> vendingMachine.vend(itemNamed: snackName)
}
</code></pre>
<p>&#x4E0A;&#x4F8B;&#x4E2D;&#xFF0C;<code>buyFavoriteSnack(person:vendingMachine:)</code> &#x51FD;&#x6570;&#x4F1A;&#x67E5;&#x627E;&#x67D0;&#x4EBA;&#x6700;&#x559C;&#x6B22;&#x7684;&#x96F6;&#x98DF;&#xFF0C;&#x5E76;&#x901A;&#x8FC7;&#x8C03;&#x7528; <code>vend(itemNamed:)</code> &#x65B9;&#x6CD5;&#x6765;&#x5C1D;&#x8BD5;&#x4E3A;&#x4ED6;&#x4EEC;&#x8D2D;&#x4E70;&#x3002;&#x56E0;&#x4E3A; <code>vend(itemNamed:)</code> &#x65B9;&#x6CD5;&#x80FD;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x8C03;&#x7528;&#x7684;&#x5B83;&#x65F6;&#x5019;&#x5728;&#x5B83;&#x524D;&#x9762;&#x52A0;&#x4E86; <code>try</code> &#x5173;&#x952E;&#x5B57;&#x3002;</p>
<p><code>throwing</code> &#x6784;&#x9020;&#x5668;&#x80FD;&#x50CF; <code>throwing</code> &#x51FD;&#x6570;&#x4E00;&#x6837;&#x4F20;&#x9012;&#x9519;&#x8BEF;&#x3002;&#x4F8B;&#x5982;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x7684; <code>PurchasedSnack</code> &#x6784;&#x9020;&#x5668;&#x5728;&#x6784;&#x9020;&#x8FC7;&#x7A0B;&#x4E2D;&#x8C03;&#x7528;&#x4E86; throwing &#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14;&#x901A;&#x8FC7;&#x4F20;&#x9012;&#x5230;&#x5B83;&#x7684;&#x8C03;&#x7528;&#x8005;&#x6765;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">PurchasedSnack</span> </span>{
    <span class="hljs-keyword">let</span> name: <span class="hljs-type">String</span>
    <span class="hljs-keyword">init</span>(name: <span class="hljs-type">String</span>, vendingMachine: <span class="hljs-type">VendingMachine</span>) <span class="hljs-keyword">throws</span> {
        <span class="hljs-keyword">try</span> vendingMachine.vend(itemNamed: name)
        <span class="hljs-keyword">self</span>.name = name
    }
}
</code></pre>
<h3 id="handling-errors-using-do-Catch">&#x7528; Do-Catch &#x5904;&#x7406;&#x9519;&#x8BEF; </h3>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x4E2A; <code>do-catch</code> &#x8BED;&#x53E5;&#x8FD0;&#x884C;&#x4E00;&#x6BB5;&#x95ED;&#x5305;&#x4EE3;&#x7801;&#x6765;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x3002;&#x5982;&#x679C;&#x5728; <code>do</code> &#x5B50;&#x53E5;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x629B;&#x51FA;&#x4E86;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x4F1A;&#x4E0E; <code>catch</code> &#x5B50;&#x53E5;&#x505A;&#x5339;&#x914D;&#xFF0C;&#x4ECE;&#x800C;&#x51B3;&#x5B9A;&#x54EA;&#x6761;&#x5B50;&#x53E5;&#x80FD;&#x5904;&#x7406;&#x5B83;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F; <code>do-catch</code> &#x8BED;&#x53E5;&#x7684;&#x4E00;&#x822C;&#x5F62;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">do</span> {
    <span class="hljs-keyword">try</span> expression
    statements
} <span class="hljs-keyword">catch</span> pattern <span class="hljs-number">1</span> {
    statements
} <span class="hljs-keyword">catch</span> pattern <span class="hljs-number">2</span> <span class="hljs-keyword">where</span> condition {
    statements
} <span class="hljs-keyword">catch</span> {
    statements
}
</code></pre>
<p>&#x5728; <code>catch</code> &#x540E;&#x9762;&#x5199;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#x6765;&#x8868;&#x660E;&#x8FD9;&#x4E2A;&#x5B50;&#x53E5;&#x80FD;&#x5904;&#x7406;&#x4EC0;&#x4E48;&#x6837;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x5982;&#x679C;&#x4E00;&#x6761; <code>catch</code> &#x5B50;&#x53E5;&#x6CA1;&#x6709;&#x6307;&#x5B9A;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x6761;&#x5B50;&#x53E5;&#x53EF;&#x4EE5;&#x5339;&#x914D;&#x4EFB;&#x4F55;&#x9519;&#x8BEF;&#xFF0C;&#x5E76;&#x4E14;&#x628A;&#x9519;&#x8BEF;&#x7ED1;&#x5B9A;&#x5230;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#x4E3A; <code>error</code> &#x7684;&#x5C40;&#x90E8;&#x5E38;&#x91CF;&#x3002;&#x5173;&#x4E8E;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x8BF7;&#x53C2;&#x8003; <a href="../chapter3/07_Patterns.html">&#x6A21;&#x5F0F;</a>&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5904;&#x7406;&#x4E86; <code>VendingMachineError</code> &#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x7684;&#x5168;&#x90E8;&#x4E09;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> vendingMachine = <span class="hljs-type">VendingMachine</span>()
vendingMachine.coinsDeposited = <span class="hljs-number">8</span>
<span class="hljs-keyword">do</span> {
    <span class="hljs-keyword">try</span> buyFavoriteSnack(person: <span class="hljs-string">&quot;Alice&quot;</span>, vendingMachine: vendingMachine)
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Success! Yum.&quot;</span>)
} <span class="hljs-keyword">catch</span> <span class="hljs-type">VendingMachineError</span>.invalidSelection {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Invalid Selection.&quot;</span>)
} <span class="hljs-keyword">catch</span> <span class="hljs-type">VendingMachineError</span>.outOfStock {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Out of Stock.&quot;</span>)
} <span class="hljs-keyword">catch</span> <span class="hljs-type">VendingMachineError</span>.insufficientFunds(<span class="hljs-keyword">let</span> coinsNeeded) {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Insufficient funds. Please insert an additional <span class="hljs-subst">\(coinsNeeded)</span> coins.&quot;</span>)
} <span class="hljs-keyword">catch</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unexpected error: <span class="hljs-subst">\(error)</span>.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Insufficient funds. Please insert an additional 2 coins.&#x201D;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>buyFavoriteSnack(person:vendingMachine:)</code> &#x51FD;&#x6570;&#x5728;&#x4E00;&#x4E2A; <code>try</code> &#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x80FD;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x3002;&#x5982;&#x679C;&#x9519;&#x8BEF;&#x88AB;&#x629B;&#x51FA;&#xFF0C;&#x76F8;&#x5E94;&#x7684;&#x6267;&#x884C;&#x4F1A;&#x9A6C;&#x4E0A;&#x8F6C;&#x79FB;&#x5230; <code>catch</code> &#x5B50;&#x53E5;&#x4E2D;&#xFF0C;&#x5E76;&#x5224;&#x65AD;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x662F;&#x5426;&#x8981;&#x88AB;&#x7EE7;&#x7EED;&#x4F20;&#x9012;&#x4E0B;&#x53BB;&#x3002;&#x5982;&#x679C;&#x9519;&#x8BEF;&#x6CA1;&#x6709;&#x88AB;&#x5339;&#x914D;&#xFF0C;&#x5B83;&#x4F1A;&#x88AB;&#x6700;&#x540E;&#x4E00;&#x4E2A; <code>catch</code> &#x8BED;&#x53E5;&#x6355;&#x83B7;&#xFF0C;&#x5E76;&#x8D4B;&#x503C;&#x7ED9;&#x4E00;&#x4E2A; <code>error</code> &#x5E38;&#x91CF;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x9519;&#x8BEF;&#x88AB;&#x629B;&#x51FA;&#xFF0C;<code>do</code> &#x5B50;&#x53E5;&#x4E2D;&#x4F59;&#x4E0B;&#x7684;&#x8BED;&#x53E5;&#x5C31;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#x3002;</p>
<p><code>catch</code> &#x5B50;&#x53E5;&#x4E0D;&#x5FC5;&#x5C06; <code>do</code> &#x5B50;&#x53E5;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x6240;&#x629B;&#x51FA;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x53EF;&#x80FD;&#x7684;&#x9519;&#x8BEF;&#x90FD;&#x4F5C;&#x5904;&#x7406;&#x3002;&#x5982;&#x679C;&#x6240;&#x6709; <code>catch</code> &#x5B50;&#x53E5;&#x90FD;&#x672A;&#x5904;&#x7406;&#x9519;&#x8BEF;&#xFF0C;&#x9519;&#x8BEF;&#x5C31;&#x4F1A;&#x4F20;&#x9012;&#x5230;&#x5468;&#x56F4;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x9519;&#x8BEF;&#x8FD8;&#x662F;&#x5FC5;&#x987B;&#x8981;&#x88AB;&#x67D0;&#x4E2A;&#x5468;&#x56F4;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x5904;&#x7406;&#x7684;&#x3002;&#x5728;&#x4E0D;&#x4F1A;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x5FC5;&#x987B;&#x7528; <code>do-catch</code> &#x8BED;&#x53E5;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x3002;&#x800C;&#x80FD;&#x591F;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x51FD;&#x6570;&#x65E2;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>do-catch</code> &#x8BED;&#x53E5;&#x5904;&#x7406;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8BA9;&#x8C03;&#x7528;&#x65B9;&#x6765;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x3002;&#x5982;&#x679C;&#x9519;&#x8BEF;&#x4F20;&#x9012;&#x5230;&#x4E86;&#x9876;&#x5C42;&#x4F5C;&#x7528;&#x57DF;&#x5374;&#x4F9D;&#x7136;&#x6CA1;&#x6709;&#x88AB;&#x5904;&#x7406;&#xFF0C;&#x4F60;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E3A;&#x4F8B;&#xFF0C;&#x4E0D;&#x662F; <code>VendingMachineError</code> &#x4E2D;&#x7533;&#x660E;&#x7684;&#x9519;&#x8BEF;&#x4F1A;&#x5728;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x5730;&#x65B9;&#x88AB;&#x6355;&#x83B7;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">nourish</span><span class="hljs-params">(with item: String)</span></span> <span class="hljs-keyword">throws</span> {
    <span class="hljs-keyword">do</span> {
        <span class="hljs-keyword">try</span> vendingMachine.vend(itemNamed: item)
    } <span class="hljs-keyword">catch</span> <span class="hljs-keyword">is</span> <span class="hljs-type">VendingMachineError</span> {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Invalid selection, out of stock, or not enough money.&quot;</span>)
    }
}

<span class="hljs-keyword">do</span> {
    <span class="hljs-keyword">try</span> nourish(with: <span class="hljs-string">&quot;Beet-Flavored Chips&quot;</span>)
} <span class="hljs-keyword">catch</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unexpected non-vending-machine-related error: <span class="hljs-subst">\(error)</span>&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Invalid selection, out of stock, or not enough money.&#x201D;</span>
</code></pre>
<p>&#x5982;&#x679C; <code>vend(itemNamed:)</code> &#x629B;&#x51FA;&#x7684;&#x662F;&#x4E00;&#x4E2A; <code>VendingMachineError</code> &#x7C7B;&#x578B;&#x7684;&#x9519;&#x8BEF;&#xFF0C;<code>nourish(with:)</code> &#x4F1A;&#x6253;&#x5370;&#x4E00;&#x6761;&#x6D88;&#x606F;&#xFF0C;&#x5426;&#x5219; <code>nourish(with:)</code> &#x4F1A;&#x5C06;&#x9519;&#x8BEF;&#x629B;&#x7ED9;&#x5B83;&#x7684;&#x8C03;&#x7528;&#x65B9;&#x3002;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x4E4B;&#x540E;&#x4F1A;&#x88AB;&#x901A;&#x7528;&#x7684; <code>catch</code> &#x8BED;&#x53E5;&#x6355;&#x83B7;&#x3002;</p>
<h3 id="converting_errors_to_optional_values">&#x5C06;&#x9519;&#x8BEF;&#x8F6C;&#x6362;&#x6210;&#x53EF;&#x9009;&#x503C; </h3>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>try?</code> &#x901A;&#x8FC7;&#x5C06;&#x9519;&#x8BEF;&#x8F6C;&#x6362;&#x6210;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x6765;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x3002;&#x5982;&#x679C;&#x662F;&#x5728;&#x8BA1;&#x7B97; <code>try?</code> &#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x8BE5;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7ED3;&#x679C;&#x5C31;&#x4E3A; <code>nil</code>&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>x</code> &#x548C; <code>y</code> &#x6709;&#x7740;&#x76F8;&#x540C;&#x7684;&#x6570;&#x503C;&#x548C;&#x7B49;&#x4EF7;&#x7684;&#x542B;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">someThrowingFunction</span><span class="hljs-params">()</span></span> <span class="hljs-keyword">throws</span> -&gt; <span class="hljs-type">Int</span> {
    <span class="hljs-comment">// ...</span>
}

<span class="hljs-keyword">let</span> x = <span class="hljs-keyword">try</span>? someThrowingFunction()

<span class="hljs-keyword">let</span> y: <span class="hljs-type">Int</span>?
<span class="hljs-keyword">do</span> {
    y = <span class="hljs-keyword">try</span> someThrowingFunction()
} <span class="hljs-keyword">catch</span> {
    y = <span class="hljs-literal">nil</span>
}
</code></pre>
<p>&#x5982;&#x679C; <code>someThrowingFunction()</code> &#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;<code>x</code> &#x548C; <code>y</code> &#x7684;&#x503C;&#x662F; <code>nil</code>&#x3002;&#x5426;&#x5219; <code>x</code> &#x548C; <code>y</code> &#x7684;&#x503C;&#x5C31;&#x662F;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x65E0;&#x8BBA; <code>someThrowingFunction()</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x662F;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#xFF0C;<code>x</code> &#x548C; <code>y</code> &#x90FD;&#x662F;&#x8FD9;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x3002;&#x4F8B;&#x5B50;&#x4E2D;&#x6B64;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6574;&#x578B;&#xFF0C;&#x6240;&#x4EE5; <code>x</code> &#x548C; <code>y</code> &#x662F;&#x53EF;&#x9009;&#x6574;&#x578B;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5BF9;&#x6240;&#x6709;&#x7684;&#x9519;&#x8BEF;&#x90FD;&#x91C7;&#x7528;&#x540C;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5904;&#x7406;&#xFF0C;&#x7528; <code>try?</code> &#x5C31;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x5199;&#x51FA;&#x7B80;&#x6D01;&#x7684;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4EE3;&#x7801;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7528;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x83B7;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x679C;&#x6240;&#x6709;&#x65B9;&#x5F0F;&#x90FD;&#x5931;&#x8D25;&#x4E86;&#x5219;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">fetchData</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">Data</span>? {
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">try</span>? fetchDataFromDisk() { <span class="hljs-keyword">return</span> data }
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">try</span>? fetchDataFromServer() { <span class="hljs-keyword">return</span> data }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}
</code></pre>
<h3 id="disabling_error_propagation">&#x7981;&#x7528;&#x9519;&#x8BEF;&#x4F20;&#x9012; </h3>
<p>&#x6709;&#x65F6;&#x4F60;&#x77E5;&#x9053;&#x67D0;&#x4E2A; <code>throwing</code> &#x51FD;&#x6570;&#x5B9E;&#x9645;&#x4E0A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x662F;&#x4E0D;&#x4F1A;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#xFF0C;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x8868;&#x8FBE;&#x5F0F;&#x524D;&#x9762;&#x5199; <code>try!</code> &#x6765;&#x7981;&#x7528;&#x9519;&#x8BEF;&#x4F20;&#x9012;&#xFF0C;&#x8FD9;&#x4F1A;&#x628A;&#x8C03;&#x7528;&#x5305;&#x88C5;&#x5728;&#x4E00;&#x4E2A;&#x4E0D;&#x4F1A;&#x6709;&#x9519;&#x8BEF;&#x629B;&#x51FA;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x65AD;&#x8A00;&#x4E2D;&#x3002;&#x5982;&#x679C;&#x771F;&#x7684;&#x629B;&#x51FA;&#x4E86;&#x9519;&#x8BEF;&#xFF0C;&#x4F60;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F7F;&#x7528;&#x4E86; <code>loadImage(atPath:)</code> &#x51FD;&#x6570;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x4ECE;&#x7ED9;&#x5B9A;&#x7684;&#x8DEF;&#x5F84;&#x52A0;&#x8F7D;&#x56FE;&#x7247;&#x8D44;&#x6E90;&#xFF0C;&#x5982;&#x679C;&#x56FE;&#x7247;&#x65E0;&#x6CD5;&#x8F7D;&#x5165;&#x5219;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x56E0;&#x4E3A;&#x56FE;&#x7247;&#x662F;&#x548C;&#x5E94;&#x7528;&#x7ED1;&#x5B9A;&#x7684;&#xFF0C;&#x8FD0;&#x884C;&#x65F6;&#x4E0D;&#x4F1A;&#x6709;&#x9519;&#x8BEF;&#x629B;&#x51FA;&#xFF0C;&#x6240;&#x4EE5;&#x9002;&#x5408;&#x7981;&#x7528;&#x9519;&#x8BEF;&#x4F20;&#x9012;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> photo = <span class="hljs-keyword">try</span>! loadImage(atPath: <span class="hljs-string">&quot;./Resources/John Appleseed.jpg&quot;</span>)
</code></pre>
<h2 id="specifying-cleanup-actions">&#x6307;&#x5B9A;&#x6E05;&#x7406;&#x64CD;&#x4F5C; </h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>defer</code> &#x8BED;&#x53E5;&#x5728;&#x5373;&#x5C06;&#x79BB;&#x5F00;&#x5F53;&#x524D;&#x4EE3;&#x7801;&#x5757;&#x65F6;&#x6267;&#x884C;&#x4E00;&#x7CFB;&#x5217;&#x8BED;&#x53E5;&#x3002;&#x8BE5;&#x8BED;&#x53E5;&#x8BA9;&#x4F60;&#x80FD;&#x6267;&#x884C;&#x4E00;&#x4E9B;&#x5FC5;&#x8981;&#x7684;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;&#xFF0C;&#x4E0D;&#x7BA1;&#x662F;&#x4EE5;&#x4F55;&#x79CD;&#x65B9;&#x5F0F;&#x79BB;&#x5F00;&#x5F53;&#x524D;&#x4EE3;&#x7801;&#x5757;&#x7684;&#x2014;&#x2014;&#x65E0;&#x8BBA;&#x662F;&#x7531;&#x4E8E;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x800C;&#x79BB;&#x5F00;&#xFF0C;&#x6216;&#x662F;&#x7531;&#x4E8E;&#x8BF8;&#x5982; <code>return</code>&#x3001;<code>break</code> &#x7684;&#x8BED;&#x53E5;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7528; <code>defer</code> &#x8BED;&#x53E5;&#x6765;&#x786E;&#x4FDD;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x5F97;&#x4EE5;&#x5173;&#x95ED;&#xFF0C;&#x4EE5;&#x53CA;&#x624B;&#x52A8;&#x5206;&#x914D;&#x7684;&#x5185;&#x5B58;&#x5F97;&#x4EE5;&#x91CA;&#x653E;&#x3002;</p>
<p><code>defer</code> &#x8BED;&#x53E5;&#x5C06;&#x4EE3;&#x7801;&#x7684;&#x6267;&#x884C;&#x5EF6;&#x8FDF;&#x5230;&#x5F53;&#x524D;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x9000;&#x51FA;&#x4E4B;&#x524D;&#x3002;&#x8BE5;&#x8BED;&#x53E5;&#x7531; <code>defer</code> &#x5173;&#x952E;&#x5B57;&#x548C;&#x8981;&#x88AB;&#x5EF6;&#x8FDF;&#x6267;&#x884C;&#x7684;&#x8BED;&#x53E5;&#x7EC4;&#x6210;&#x3002;&#x5EF6;&#x8FDF;&#x6267;&#x884C;&#x7684;&#x8BED;&#x53E5;&#x4E0D;&#x80FD;&#x5305;&#x542B;&#x4EFB;&#x4F55;&#x63A7;&#x5236;&#x8F6C;&#x79FB;&#x8BED;&#x53E5;&#xFF0C;&#x4F8B;&#x5982; <code>break</code>&#x3001;<code>return</code> &#x8BED;&#x53E5;&#xFF0C;&#x6216;&#x662F;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;&#x5EF6;&#x8FDF;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#x4F1A;&#x6309;&#x7167;&#x5B83;&#x4EEC;&#x58F0;&#x660E;&#x7684;&#x987A;&#x5E8F;&#x4ECE;&#x540E;&#x5F80;&#x524D;&#x6267;&#x884C;&#x2014;&#x2014;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x7B2C;&#x4E00;&#x6761; <code>defer</code> &#x8BED;&#x53E5;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x6700;&#x540E;&#x624D;&#x6267;&#x884C;&#xFF0C;&#x7B2C;&#x4E8C;&#x6761; <code>defer</code> &#x8BED;&#x53E5;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x5012;&#x6570;&#x7B2C;&#x4E8C;&#x4E2A;&#x6267;&#x884C;&#xFF0C;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#x3002;&#x6700;&#x540E;&#x4E00;&#x6761;&#x8BED;&#x53E5;&#x4F1A;&#x7B2C;&#x4E00;&#x4E2A;&#x6267;&#x884C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">processFile</span><span class="hljs-params">(filename: String)</span></span> <span class="hljs-keyword">throws</span> {
    <span class="hljs-keyword">if</span> exists(filename) {
        <span class="hljs-keyword">let</span> file = open(filename)
        <span class="hljs-keyword">defer</span> {
            close(file)
        }
        <span class="hljs-keyword">while</span> <span class="hljs-keyword">let</span> line = <span class="hljs-keyword">try</span> file.readline() {
            <span class="hljs-comment">// &#x5904;&#x7406;&#x6587;&#x4EF6;&#x3002;</span>
        }
        <span class="hljs-comment">// close(file) &#x4F1A;&#x5728;&#x8FD9;&#x91CC;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x5373;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x6700;&#x540E;&#x3002;</span>
    }
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F7F;&#x7528;&#x4E00;&#x6761; <code>defer</code> &#x8BED;&#x53E5;&#x6765;&#x786E;&#x4FDD; <code>open(_:)</code> &#x51FD;&#x6570;&#x6709;&#x4E00;&#x4E2A;&#x76F8;&#x5E94;&#x7684;&#x5BF9; <code>close(_:)</code> &#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5373;&#x4F7F;&#x6CA1;&#x6709;&#x6D89;&#x53CA;&#x5230;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>defer</code> &#x8BED;&#x53E5;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="16_Optional_Chaining.html" class="navigation navigation-prev " aria-label="Previous page: 可选链">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="18_Type_Casting.html" class="navigation navigation-next " aria-label="Next page: 类型转换">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"错误处理","level":"1.3.17","depth":2,"next":{"title":"类型转换","level":"1.3.18","depth":2,"path":"chapter2/18_Type_Casting.md","ref":"chapter2/18_Type_Casting.md","articles":[]},"previous":{"title":"可选链","level":"1.3.16","depth":2,"path":"chapter2/16_Optional_Chaining.md","ref":"chapter2/16_Optional_Chaining.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/17_Error_Handling.md","mtime":"2019-07-05T01:01:08.098Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

